package com.awanp3.nimbus.tool.asset.service.impl;

import com.awanp3.nimbus.common.core.utils.MapstructUtils;
import com.awanp3.nimbus.common.core.utils.StringUtils;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.awanp3.nimbus.tool.asset.domain.bo.HostBo;
import com.awanp3.nimbus.tool.asset.domain.vo.HostVo;
import com.awanp3.nimbus.tool.asset.domain.Host;
import com.awanp3.nimbus.tool.asset.mapper.HostMapper;
import com.awanp3.nimbus.tool.asset.service.IHostService;

import java.util.List;
import java.util.Collection;

/**
 * 主机Service业务层处理
 *
 * @author 20014170
 * @date 2025-08-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HostServiceImpl implements IHostService {

    private final HostMapper baseMapper;

    /**
     * 查询主机
     *
     * @param id 主键
     * @return 主机
     */
    @Override
    public HostVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询主机列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 主机分页列表
     */
    @Override
    public TableDataInfo<HostVo> queryPageList(HostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Host> lqw = buildQueryWrapper(bo);
        Page<HostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的主机列表
     *
     * @param bo 查询条件
     * @return 主机列表
     */
    @Override
    public List<HostVo> queryList(HostBo bo) {
        LambdaQueryWrapper<Host> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Host> buildQueryWrapper(HostBo bo) {
        LambdaQueryWrapper<Host> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(Host::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getTypes()), Host::getTypes, bo.getTypes());
        lqw.eq(StringUtils.isNotBlank(bo.getOsType()), Host::getOsType, bo.getOsType());
        lqw.eq(StringUtils.isNotBlank(bo.getArchType()), Host::getArchType, bo.getArchType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Host::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), Host::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), Host::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), Host::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Host::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增主机
     *
     * @param bo 主机
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HostBo bo) {
        Host add = MapstructUtils.convert(bo, Host.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改主机
     *
     * @param bo 主机
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HostBo bo) {
        Host update = MapstructUtils.convert(bo, Host.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Host entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除主机信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
